package com.mp.approvalreq.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.entity.Query;
import com.mp.approvalreq.entity.Request;
import com.mp.approvalreq.service.RequestServicePrx;
import com.mp.user.entity.User;
import com.plusmoney.exception.RpcServerLogicalException;
import com.plusmoney.util.Utils;
import com.qq.tars.client.util.ServantUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 获得审批详情
 *
 * @author Administrator
 */
@WebServlet("/getRequestDetail")
public class GetRequestDetail extends AbstractServlet {

    private static final Logger logger = LoggerFactory.getLogger(GetRequestDetail.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        try {
            User user = getReqUser(req);

            Request request = getRequest(req);

            check(request);

            parseCookies(req, request);

            request.setVersionInfo(getVersionInfo(req));


            RequestServicePrx requestServicePrx = ServantUtils.getObject(RequestServicePrx.class);
            Query query = getQuery(req, user, request);
            request = requestServicePrx.getRequestDetail(query);

            JSONObject jsonStr = new JSONObject();

            returnResult(resp, request, jsonStr);
        } catch (Throwable e) {
            onThrowable(req, resp, e);
        }
    }

    private void parseCookies(HttpServletRequest req, Request request) {
        try {
            Cookie[] cookies = req.getCookies();
            if (Utils.isNotNull(cookies)) {
                for (Cookie cookie : cookies) {
                    if ("withoutContact".equals(cookie.getName())) {
                        request.setWithoutContact(Integer.parseInt(cookie.getValue()));
                        return;
                    }
                }
            }
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
        }
    }


    private void returnResult(HttpServletResponse resp, Request request, JSONObject jsonStr) throws IOException {
        jsonStr.put("result", "0");
        jsonStr.put("res_info", "获得成功");
        jsonStr.put("data", request);

        logger.info(JSON.toJSONString(jsonStr));

        printJson(resp, jsonStr.toJSONString());
    }

    private Request getRequest(HttpServletRequest req) throws IOException {
        String reqBody = getContent(req);
        logger.info("json参数 {}", reqBody);

        Request request;
        try {
            request = JSONObject.parseObject(reqBody, Request.class);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new RpcServerLogicalException(1, "查看审批失败，请稍候再试");
        }
        return request;
    }

    protected void check(Request request) {
        if (request.getRequestDkey() <= 0) {
            throw new RpcServerLogicalException(1, "请求ID不正确");
        }
    }

}
